package com.code.generation.utils;

import org.springframework.core.io.ClassPathResource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ProjectName: codegeneration
 * @Package: com.code.generation.utils
 * @Description:
 * @Author: DongJW
 * @CreateDate: 2020/11/29 11:38
 * @Version: 1.0
 */
public class PropertitesUtil {
    public static Map<String,String> propsMap = new ConcurrentHashMap<>();

    static {
        try {
            readPropertiesFile("/myproperties.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readPropertiesFile(String filePath) throws FileNotFoundException, IOException {
        try {
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            InputStream inputStream =classPathResource.getInputStream();
            Properties props = new Properties();
            props.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            propsMap.putAll((Map)props);
            System.out.println("读取myproperties.properties文件");
        } catch (Exception e) {
            System.out.println("读取 myproperties.properties文件失败！");
            e.printStackTrace();
        }
    }
}
